package com.key.custom.plm.service.serviceImp;

import org.hibernate.Query;
import org.hibernate.Session;

import com.key.base.service.serviceImp.BaseServiceImp;
import com.key.custom.plm.dao.IHrPLMDao;
import com.key.custom.plm.model.HrPLM;
import com.key.custom.plm.service.IHrPLMService;
import com.key.util.HibernateUtil;

public class HrPLMServiceImp extends BaseServiceImp implements IHrPLMService {
	IHrPLMDao hrPLMDao;

	public IHrPLMDao getHrPLMDao() {
		return hrPLMDao;
	}

	public void setHrPLMDao(IHrPLMDao hrPLMDao) {
		this.hrPLMDao = hrPLMDao;
	}

	/**
	 * model
	 * 
	 * @param hrPLM
	 */
	@Override
	public void add(HrPLM hrPLM) throws Exception {
		try {
			hrPLMDao.add(hrPLM);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public void deleteAllObj(String className, String[] ids) throws Exception {
		Session session = HibernateUtil.getHrIntSessionFactory().openSession();
		try {
			StringBuffer queryHQL = new StringBuffer();
			session.beginTransaction();
			for (int i = 0; i < ids.length; i++) {
				String id = ids[i];
				String hql = "delete from " + className + " where id='" + id
						+ "'";
				System.out.println(hql);
				Query query = session.createQuery(hql);
				System.out.println(query.executeUpdate());
			}
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			throw e;
		} finally {
			session.flush();
			session.close();
		}
	}

	/**
	 * model
	 * 
	 * @param hrPLM
	 */
	@Override
	public void update(HrPLM hrPLM) throws Exception {
		try {
			hrPLMDao.update(hrPLM);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
}
